package com.syaccp.controller;

import com.github.pagehelper.PageInfo;
import com.syaccp.model.TCustomerBill;
import com.syaccp.model.TWithdrawalDetails;
import com.syaccp.service.impl.TCustomerBillServiceImpl;
import com.syaccp.service.impl.TWithdrawalDetailsServiceImpl;
import com.syaccp.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import sun.plugin.viewer.context.IExplorerAppletContext;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@CrossOrigin
@RequestMapping("/TStoreCoupon")
@RestController
public class TSoreCouponController {

    @Resource
    TCustomerBillServiceImpl tCustomerBillService;

    @Resource
    private TWithdrawalDetailsServiceImpl tWithdrawalDetailsService;

    //查询所有体现信息
    @GetMapping("tWithdrawalDetails")
    @ApiOperation(value = "查询提现信息")
    public Result tWithdrawalDetails(Integer index, TWithdrawalDetails tWithdrawalDetails) throws SQLException {
        System.out.println("index = " + index);
        System.out.println("tWithdrawalDetails = " + tWithdrawalDetails);
        PageInfo<TWithdrawalDetails> tWithdrawalDetails1 = tWithdrawalDetailsService.PagingQueryWithdrawal(index,tWithdrawalDetails);
        return Result.success(tWithdrawalDetails1);
    }

    @GetMapping("tCustomerBill")
    @ApiOperation(value = "查询资金流水")
    public Result tCustomerBill(TCustomerBill tCustomerBill) throws SQLException {
        PageInfo<TCustomerBill> tCustomerBills =  tCustomerBillService.pageSelectAll(tCustomerBill.getIndex(),tCustomerBill);
        return Result.success(tCustomerBills);
    }

    @PutMapping("/Etf")
    @ApiOperation(value = "通过按钮的方法",tags = "转账方法，三个表格修改")
    @Transactional
    public Result etf(@RequestBody TWithdrawalDetails tWithdrawalDetails) throws SQLException {
        if (tWithdrawalDetailsService.updateDept(tWithdrawalDetails) == 1) {
            int update = tWithdrawalDetailsService.updateStatus(tWithdrawalDetails);
            int i = tWithdrawalDetailsService.updateCustomer(tWithdrawalDetails);
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @PutMapping("/wcb")
    @ApiOperation(value = "公司余额提现")
    @Transactional
    public Result wcb(@RequestBody TWithdrawalDetails tWithdrawalDetails1) throws SQLException {
        System.out.println(tWithdrawalDetails1.getMoney()+"+++++"+tWithdrawalDetails1.getBankAccount()+"=====");
        TWithdrawalDetails tWithdrawalDetails = tWithdrawalDetailsService.WithdrawalOfCompanyBalance(tWithdrawalDetails1);
        tWithdrawalDetails.setMoney(tWithdrawalDetails1.getMoney());
        System.out.println(tWithdrawalDetails.getMoney()+"++++++"+tWithdrawalDetails.getBankAccount());
        if (tWithdrawalDetailsService.updateDept1(tWithdrawalDetails)==1){
            int i = tWithdrawalDetailsService.updateCustomer1(tWithdrawalDetails);
            return Result.success();
        }else{
            return Result.error();
        }
    }


    @GetMapping(value = "/WithdrawalDetails")
    public Result FindByEmpMsg(Integer status) {
        List<TWithdrawalDetails> withdrawalDetails1 = tWithdrawalDetailsService.queryCondition(status);
        return Result.success(withdrawalDetails1);
    }


    @RequestMapping(value = "/information")
    public Result information() {
        TWithdrawalDetails information = tWithdrawalDetailsService.information();
        if (information != null) {
            information.setId(1);
        }
        return Result.success(information);
    }
    @PutMapping(value = "alterTheState")
    public Result alterTheState(@RequestBody TWithdrawalDetails withdrawalDetails){
        int i = tWithdrawalDetailsService.alterTheState(withdrawalDetails);
        return Result.success(i);

    }

    @PutMapping("/Refuse")
    public Result refuse (@RequestBody TWithdrawalDetails withdrawalDetails) throws SQLException {
        Result result = new Result();
        int i = tWithdrawalDetailsService.insertRemark(withdrawalDetails);
        int i1 = tWithdrawalDetailsService.updateState(withdrawalDetails);
        result.setData(i);
        result.setData(i1);
        return Result.success(result);
    }





}
